package cn.btks.zuulserver.util;

import cn.btks.commonserver.util.sm2.SM2EncDecUtils;
import cn.btks.zuulserver.dto.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lijun
 * @Date 2021/8/5 10:06
 */
public class DecryptTel {

    public static User decryptTele(User user) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        User user1 = objectMapper.readValue(objectMapper.writeValueAsString(user), User.class);
        if(!StringUtils.isEmpty(user.getTelephone())){
            user1.setTelephone(SM2EncDecUtils.decrypt(user.getTelephone()));
        }
        return user1;
    }

    public static List<User> decryptTeleList(List<User> userList) throws IOException {
        List<User> userList1 = new ArrayList<>();
        if(!CollectionUtils.isEmpty(userList)){
            for (User item : userList) {
                User user = decryptTele(item);
                userList1.add(user);
            }
        }
        return userList1;
    }
}
